package com.nnnu.wsnackshop.controller;

import com.nnnu.wsnackshop.controller.base.BaseController;
import com.nnnu.wsnackshop.pojo.dto.RiderStatusDTO;
import com.nnnu.wsnackshop.pojo.vo.RiderVO;
import com.nnnu.wsnackshop.result.PageResult;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.IRidersService;
import com.nnnu.wsnackshop.utils.JwtUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 骑手表 前端控制器
 * </p>
 *
 * @author zk
 * @since 2025-05-14
 */
@RestController("ridersRidersController")
@RequestMapping("/rider")
@Slf4j
@RequiredArgsConstructor
@RequiresRoles("rider")
public class RidersController extends BaseController {

    private final IRidersService ridersService;

    /**
     * 获取骑手ID
     */
    protected Integer getRiderIdFromToken() {
        String token = (String) SecurityUtils.getSubject().getPrincipal();
        Integer riderId = JwtUtil.getRiderIdFromToken(token);
        if (riderId == null) {
            log.error("无法从token中获取骑手ID");
            throw new RuntimeException("无法获取骑手信息，请重新登录");
        }
        return riderId;
    }

    /**
     * 获取骑手详情
     */
    @GetMapping("/profile")
    public Result<RiderVO> getRiderProfile() {
        Integer riderId = getRiderIdFromToken();
        log.info("获取骑手个人信息: riderId={}", riderId);
        RiderVO rider = ridersService.getRiderDetail(riderId);
        return Result.success(rider);
    }

    /**
     * 更新骑手状态
     */
    @PutMapping("/status")
    public Result<Boolean> updateStatus(@RequestBody RiderStatusDTO statusDTO) {
        Integer riderId = getRiderIdFromToken();
        log.info("更新骑手状态: riderId={}, status={}", riderId, statusDTO.getStatus());
        boolean success = ridersService.updateRiderStatus(riderId, statusDTO.getStatus());
        return Result.success(success);
    }

    /**
     * 获取骑手的订单列表
     */
    @GetMapping("/orders")
    public Result<PageResult<Map<String, Object>>> getOrders(
            @RequestParam(required = false) String status,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        Integer riderId = getRiderIdFromToken();
        log.info("获取骑手订单列表: riderId={}, status={}, page={}, pageSize={}", riderId, status, page, pageSize);
        PageResult<Map<String, Object>> orders = ridersService.getRiderOrders(riderId, status, page, pageSize);
        return Result.success(orders);
    }

    /**
     * 骑手接单
     */
    @PostMapping("/orders/{orderId}/accept")
    public Result<Boolean> acceptOrder(@PathVariable Long orderId) {
        Integer riderId = getRiderIdFromToken();
        log.info("骑手接单: riderId={}, orderId={}", riderId, orderId);
        boolean success = ridersService.acceptOrder(riderId, orderId);
        return Result.success(success);
    }

    /**
     * 骑手确认送达
     */
    @PostMapping("/orders/{orderId}/deliver")
    public Result<Boolean> confirmDelivery(@PathVariable Long orderId) {
        Integer riderId = getRiderIdFromToken();
        log.info("骑手确认送达: riderId={}, orderId={}", riderId, orderId);
        boolean success = ridersService.confirmDelivery(riderId, orderId);
        return Result.success(success);
    }
}
